package com.qunar.flight.myserver;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;

public class RequestHandler2 implements Handler {
	private SocketChannel sc;
	private SelectionKey sk;

	RequestHandler2(SocketChannel sc) {
		this.sc = sc;
	}

	public void setSk(SelectionKey sk) {
		this.sk = sk;
	}

	public void handle(SelectionKey sk){
		try {
			System.out.print(getClass());
			ByteBuffer requestBB = ByteBuffer.allocate(1024 * 4);
			sc.read(requestBB);
			System.out.println("请求字符串" + new String(requestBB.array()).trim());
			sc.write(Charset.forName("GBK").encode("简单字符串"));
			sc.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void run() {
		try {
			handle(sk);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
